package com.sm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sm.dao.*;
import com.sm.entity.*;
import com.sm.service.AdminService;
import com.sm.utils.PageUtils;
import com.sm.utils.Query;
import com.sm.vo.CourseListVo;
import com.sm.vo.StudentVo;
import com.sm.vo.TeacherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author Lenovo
 */
@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    CourseListDao courseListDao;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ClassMapper classMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                new QueryWrapper<AdminEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean addStudent(Student student) {
        int insert = studentMapper.insert(student);
        return insert == 1;
    }

    @Override
    public boolean deleteStudent(Integer id) {
        Student student = studentMapper.selectById(id);
        String phone = student.getPhone();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone).eq("role", "student");
        int delete1 = userMapper.delete(wrapper);

        int delete = studentMapper.deleteById(id);
        return delete == 1 && delete1 == 1;
    }

    @Override
    public List<StudentVo> getAllStudentByPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (key != null) {
            wrapper.like("name", key).or().like("id", key);
        }
        List<Student> students = studentMapper.selectList(wrapper);
        if (students != null) {
            List<StudentVo> collect = students.stream().map(item -> {
                StudentVo vo = new StudentVo();
                BeanUtils.copyProperties(item, vo);

                QueryWrapper<User> userWrapper = new QueryWrapper<>();
                userWrapper.eq("phone", item.getPhone());
                User user = userMapper.selectOne(userWrapper);
                if (user != null) {
                    vo.setUsername(user.getUsername());
                    vo.setGender(user.getGender());
                }

                QueryWrapper<ClassEntity> classWrapper = new QueryWrapper<>();
                classWrapper.eq("student_id", item.getId());
                ClassEntity classEntity = classMapper.selectOne(classWrapper);
                if (classEntity != null) {
                    vo.setClassName(classEntity.getClassName());
                }
                return vo;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    @Override
    public boolean addTeacher(Teacher teacher) {
        int insert = teacherMapper.insert(teacher);
        return insert == 1;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public boolean deleteTeacher(Integer id) {
        Teacher teacher = teacherMapper.selectById(id);
        String phone = teacher.getPhone();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone).eq("role", "teacher");
        int delete1 = userMapper.delete(wrapper);

        int delete = teacherMapper.deleteById(id);
        return delete == 1 && delete1 == 1;
    }

    @Override
    public boolean updateTeacher(TeacherVo teacherVo) {
        String phone = teacherVo.getPhone();
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherVo, teacher);
        int update = teacherMapper.updateById(teacher);

        User user = new User();
        user.setGender(teacherVo.getGender());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        int update1 = userMapper.update(user, wrapper);

        return update == 1 && update1 == 1;
    }

    @Override
    public boolean addCourse(CourseListEntity courseList) {
        int insert = courseListDao.insert(courseList);
        return insert == 1;
    }

    @Override
    public boolean deleteCourse(Integer id) {
        int delete = courseListDao.deleteById(id);
        return delete == 1;
    }

    @Override
    public boolean updateCourse(CourseListEntity courseList) {
        int update = courseListDao.updateById(courseList);
        return update == 1;
    }

    @Override
    public List<Teacher> getAllTeachers() {
        return teacherMapper.selectList(null);
    }

    @Override
    public List<User> getAllAdmin(Map<String, Object> params) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("role", "admin");
        List<User> users = userMapper.selectList(wrapper);
        if (users != null) {
            return users;
        }
        return null;
    }

    @Override
    public boolean addAdmin(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        Integer count = userMapper.selectCount(wrapper);
        if (count != 0) {
            return false;
        } else {
            user.setRole("admin");
            userMapper.insert(user);
            return true;
        }
    }

    @Override
    public boolean updateAdmin(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        Integer count = userMapper.selectCount(wrapper);
        if (count != 0) {
            return false;
        } else {
            userMapper.updateById(user);
            return true;
        }
    }

    @Override
    public boolean deleteAdmin(Integer id) {
        int delete = userMapper.deleteById(id);
        return delete == 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStudent(StudentVo studentVo) {
        Integer id = studentVo.getId();

        Student student = new Student();
        BeanUtils.copyProperties(studentVo, student);
        studentMapper.updateById(student);

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", studentVo.getPhone());
        User user = new User();
        user.setGender(studentVo.getGender());
        user.setRealName(student.getName());
        user.setAddress(studentVo.getAddress());
        userMapper.update(user, wrapper);

        QueryWrapper<ClassEntity> classWrapper = new QueryWrapper<>();
        classWrapper.eq("student_id", id);
        ClassEntity classEntity = classMapper.selectOne(classWrapper);
        if (classEntity != null) {
            classEntity.setClassName(studentVo.getClassName());
            classMapper.updateById(classEntity);
        }
        return true;
    }

    @Override
    public List<TeacherVo> getAllTeachers(Map<String, Object> params) {
        List<Teacher> teachers = teacherMapper.selectList(null);
        if (teachers != null) {
            List<TeacherVo> collect = teachers.stream().map(item -> {
                TeacherVo vo = new TeacherVo();
                BeanUtils.copyProperties(item, vo);

                QueryWrapper<User> wrapper = new QueryWrapper<>();
                wrapper.eq("phone", item.getPhone()).eq("role", "teacher");
                User user = userMapper.selectOne(wrapper);

                vo.setGender(user.getGender());
                return vo;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    @Override
    public List<CourseListVo> getAllCourse(Map<String, Object> params) {
        List<CourseListEntity> courseListEntities = courseListDao.selectList(null);
        if (courseListEntities != null) {
            List<CourseListVo> collect = courseListEntities.stream().map(item -> {
                Integer teacherId = item.getTeacherId();
                Teacher teacher = teacherMapper.selectById(teacherId);

                CourseListVo vo = new CourseListVo();
                if (teacher != null) {
                    vo.setTeacher(teacher);
                }

                BeanUtils.copyProperties(item, vo);
                return vo;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }


}